package GUI;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

import voting.ClientVoting;

import entities.*;
@SuppressWarnings("serial")
public class VotingPanel extends JPanel implements MouseListener,ActionListener
{
	JButton vote;
	JButton cancel;
	CandidatePanel [] candidatePanel;
	JLabel next;
	JLabel back;
	JLabel intro;
	int currentPanel;
	Candidate [] candidates;
	Voting v;
	public VotingPanel(Voting v,Candidate [] candidates)
	{
		this.v = v;
		this.candidates = candidates;
		setBounds(0,0,750,650);
		setLayout(null);
		
		intro = new JLabel(v.getQuestions());
		intro . setFont(new Font(Font.SANS_SERIF,Font.BOLD, 30));
		intro . setBounds(10,10,600,50);
		add(intro);
		
		candidatePanel = new CandidatePanel[candidates.length];	
		for(int i = 0 ; i<candidatePanel.length ; ++i)
		{
			candidatePanel[i] = new CandidatePanel(candidates[i].getName(),candidates[i].getProgram(),null);
			candidatePanel[i] . setLocation(45,150);
		}
		
		next = new JLabel(new ImageIcon("right.png"));
		next . setBounds(650,80,72,60);
		next . addMouseListener(this);
		add(next);
		
		back = new JLabel(new ImageIcon("left.png"));
		back . setBounds(20,80,72,60);
		back . addMouseListener(this);
		back . setVisible(false);
		add(back);
		
		cancel = new JButton("Exit Voting");
		cancel . setFont(new Font(null, Font.BOLD, 15));
		cancel . setBounds(45,470,200,30);
		cancel . setBackground(Color.white);
		cancel . addActionListener(this);
		add(cancel);
		
		vote = new JButton("Vote For "+candidates[0].getName());
		vote . setFont(new Font(null,Font.BOLD,15));
		vote . setBounds(400,470,295,30);
		vote . setBackground(Color.white);
		vote . addActionListener(this);
		add(vote);
		
		add(candidatePanel[0]);
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		next.setVisible(true);
		back.setVisible(true);
		if(arg0.getSource() == next)
		{
			remove(candidatePanel[currentPanel]);
			currentPanel = Math.min(currentPanel+1,candidatePanel.length-1);
			add(candidatePanel[currentPanel]);
		}
		else
		{
			remove(candidatePanel[currentPanel]);
			currentPanel = Math.max(currentPanel-1,0);
			add(candidatePanel[currentPanel]);
		}
		if(currentPanel == 0)
			back.setVisible(false);
		if(currentPanel == candidatePanel.length-1)
			next.setVisible(false);
		vote.setText("Vote For "+candidates[currentPanel].getName());
		validate();
		repaint();
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource()  == vote)
		{
			ClientVoting.votingChosen = v;
			ClientVoting.candidates = candidates;
			String doVote = ClientVoting.makeVote(new int [] {candidates[currentPanel].getCandidateID()});
			if(doVote == null)
			JOptionPane.showMessageDialog(null, "Problem occurred please try to vote later");
			else
			{
				if(doVote.charAt(0) == 'T')
				JOptionPane.showMessageDialog(null, "Your old Vote is updated. You new Vote is "+doVote.substring(1));
				else
				JOptionPane.showMessageDialog(null, "Thank you for voting, Your Vote is "+doVote);
				MainFrame.getMainFrame().remove(this);
				MainFrame.getMainFrame().addViewVotingPnl(false);
			}
		}
		if(arg0.getSource() == cancel)
		{
			MainFrame.getMainFrame().remove(this);
			MainFrame.getMainFrame().addViewVotingPnl(false);
		}
	}
}
